// src/stores/rateLimiter.js
import { defineStore } from 'pinia'
import {
    getRateLimiterConfig,
    updateRateLimiterConfig
} from '@/api/rateLimiter'

export const useRateLimiterStore = defineStore('rateLimiter', {
    state: () => ({
        config: {
            maxConcurrentPerDomain: 1,
            globalConcurrent: 3,
            minInterval: 3000,
            maxInterval: 8000,
            domainInterval: 3000
        },
        loading: false
    }),

    actions: {
        async fetchConfig() {
            this.loading = true
            try {
                const response = await getRateLimiterConfig()
                this.config = response.data.data
            } catch (error) {
                throw error
            } finally {
                this.loading = false
            }
        },

        async updateConfig(config) {
            try {
                await updateRateLimiterConfig(config)
                await this.fetchConfig()
                return true
            } catch (error) {
                throw error
            }
        }
    }
})